package dhbw.GUI.Panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import Main.MyController;
import dhbw.Data.E_Address;
import dhbw.Data.E_Role;
import dhbw.Data.Kurs;
import dhbw.Data.Language;
import dhbw.Data.Settings;
import dhbw.Roles.Dozent;
import dhbw.Roles.Professor;
import dhbw.Roles.Student;
import dhbw.Roles.Studiengangsleiter;
import dhbw.Roles.User;

public class PanelNewUser extends JPanel implements ActionListener {

	private boolean writeable;
	private MyController myc;
	private JFrame parent;
	private JButton bt_add, bt_cancel;
	private ContentPanelAddress addresses;
	private ContentPanelRoleSelection roleSelection;
	private ContentPanelLoginData loginData;

	public PanelNewUser(boolean writeable, MyController myc, JFrame parent) {
		this.writeable = writeable;
		this.myc = myc;
		this.parent = parent;
		build();
	}

	private void build() {
		this.setBackground(Settings.BG_COLOR);
		this.setLayout(new BorderLayout());
		bt_cancel = new JButton(Language.CANCEL[myc.getLanguage()]);

		bt_add = new JButton(Language.SAVE[myc.getLanguage()]);
		bt_cancel.addActionListener(this);

		bt_add.addActionListener(this);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Settings.BG_COLOR);
		buttonPanel.add(bt_cancel);
		buttonPanel.add(bt_add);
		addresses = new ContentPanelAddress(this.myc);
		roleSelection = new ContentPanelRoleSelection(this.myc);
		loginData = new ContentPanelLoginData(this.myc);

		this.add("West", addresses);
		JPanel eastPanel = new JPanel();
		eastPanel.setLayout(new BorderLayout());
		eastPanel.add("Center", roleSelection);
		eastPanel.add("North", loginData);
		this.add("East", eastPanel);
		this.add("South", buttonPanel);
		Studiengangsleiter stgl = ((Studiengangsleiter) this.myc
				.getActiveUser());
		for (int i = 0; i < stgl.getStudyPath().getKurse().size(); i++) {
			roleSelection.cb_Kurs
					.addItem(stgl.getStudyPath().getKurse().get(i));
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(bt_cancel)) {
			this.parent.dispose();
		}
		if (arg0.getSource().equals(bt_add)) {
			if (filledRight(addresses) && filledRight(roleSelection)
					&& filledRight(loginData)) {
				JOptionPane.showMessageDialog(this.getComponent(0), new String(
						Language.FIELDSOKNEWUSER[this.myc.getLanguage()]),
						Language.INFO[this.myc.getLanguage()], 1);
				Studiengangsleiter stgl = ((Studiengangsleiter) this.myc
						.getActiveUser());

				E_Address ad = new E_Address(loginData.tf_login.getText(),
						addresses.tf_street.getText(),
						addresses.tf_city.getText(),
						addresses.tf_zipcode.getText(),
						addresses.tf_email.getText(),
						addresses.tf_phone.getText(),
						addresses.tf_mobile.getText());

				if (this.roleSelection.radioStudent.isSelected()) {
					Student s1 = new Student(loginData.tf_login.getText(),
							addresses.tf_name.getText(),
							addresses.tf_vorname.getText(), ad,
							loginData.tf_login.getText(),
							loginData.tf_password.getText(),
							Color.decode("#FFFFFF"), 0);
					s1.setRole(new E_Role(loginData.tf_login.getText(),
							"Student", false));
					if(!roleSelection.tf_kurs.getText().trim().equals(""))
					{
						Kurs k1 = new Kurs(roleSelection.tf_kurs.getText());
						k1.setStudyPath(((Studiengangsleiter)this.myc.getActiveUser()).getStudyPath());
						((Studiengangsleiter)this.myc.getActiveUser()).getStudyPath().addKurs(k1);
						s1.setCourse(k1);
					}
					else s1.setCourse((Kurs) roleSelection.cb_Kurs.getSelectedItem());
					stgl.newUser(s1, this.myc);
				}
				if (this.roleSelection.radioDozent.isSelected()) {
					Dozent d1 = new Dozent(loginData.tf_login.getText(),
							addresses.tf_name.getText(),
							addresses.tf_vorname.getText(), ad,
							loginData.tf_login.getText(),
							loginData.tf_password.getText(),
							stgl.getStudyPath(), Color.decode("#FFFFFF"), 0);
					d1.setRole(new E_Role(loginData.tf_login.getText(),
							"Student", false));
					d1.setStudyPath(stgl.getStudyPath());
					stgl.newUser(d1, this.myc);
				}
				if (this.roleSelection.radioProfessor.isSelected()) {
					Professor p1 = new Professor(loginData.tf_login.getText(),
							addresses.tf_name.getText(),
							addresses.tf_vorname.getText(), ad,
							loginData.tf_login.getText(),
							loginData.tf_password.getText(),
							stgl.getStudyPath(), Color.decode("#FFFFFF"), 0);
					p1.setRole(new E_Role(loginData.tf_login.getText(),
							"Student", false));
					p1.setStudyPath(stgl.getStudyPath());
					stgl.newUser(p1, this.myc);
				}
				parent.dispose();
			} else {
				JOptionPane.showMessageDialog(this.getComponent(0), new String(
						Language.FIELDSAREEMPTY[this.myc.getLanguage()]),
						Language.ERROR[this.myc.getLanguage()], 0);
			}
		}
	}

	public boolean filledRight(JPanel panel) {
		boolean rtn = true;
		Component[] components = panel.getComponents();
		Component component = null;
		for (int i = 0; i < components.length; i++) {
			component = components[i];
			if (component instanceof JTextField) {
				if (!((JTextField) component).equals(roleSelection.tf_kurs)) {
					String text = ((JTextField) component).getText();
					if (text.equals("") || text.trim().equals("")) {
						rtn = false;
					}
				}
			}
		}
		return rtn;
	}
}
